package com.cssl.springbootdemo01.controller;

import com.cssl.springbootdemo01.pojo.Users;
import com.cssl.springbootdemo01.service.UsersService;
import com.mongodb.client.result.DeleteResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.jws.soap.SOAPBinding;
import java.util.List;
import java.util.regex.Pattern;

@RestController
//@Controller
public class DataController {
    @Autowired
    private UsersService usersService;

    @Autowired
    public MongoTemplate mongoTemplate;

    @GetMapping("/findusers")
    public List<Users> findAll(){
        return usersService.find();
    }

    @GetMapping("/insert/{id}")
    public boolean insert(@PathVariable int id){
        return usersService.insert(id);
    }

    @GetMapping("/delete")
    public boolean delete(@PathVariable int id){
        return usersService.delete(id);
    }

    @GetMapping("/findbyid/{id}")
    public Users findByid(@PathVariable int id) {
        System.out.println("findByid:"+id);
        System.out.println(usersService.findByid(id));
        return usersService.findByid(id);
    }

    @GetMapping("/eDelete/{id}")
    public boolean eDelete(@PathVariable int id){
        System.out.println("edelete:"+id);
        System.out.println(usersService.eDelete(id));
        return usersService.eDelete(id);
    }


    ///mongodb

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/findAllmd")
    public List<Users> findAllmd(){
        //return usersService.findAll();
        return mongoTemplate.findAll(Users.class);
    }

    /**
     * 保存mongodb/修改
     * @param users
     * @return
     */
    @GetMapping("save")
    public Users save(Users users){
        //相同的id会替换掉
        System.out.println("save"+users);
       //return usersService.saveUsers(users);
        return mongoTemplate.insert(users);
    }

    /**
     * 根据名字查询
     * @param uname
     * @return
     */
    @GetMapping("/findByUname")
    public List<Users> findByUname(String uname){
        System.out.println("findByUname"+uname);
        //return usersService.findByUname(uname);
        Query query=new Query(Criteria.where("uname").is(uname));
        return mongoTemplate.find(query,Users.class);
    }

    /**
     * 删除
     * @return
     */
    @GetMapping("/removemd")
    public void deletemd(int uid){
         usersService.removeUsers(uid);
        System.out.println("删除成功"+uid);
    }


    /**
     * 删除
     * @return
     */
    @GetMapping("/removete")
    public DeleteResult deletemd2(Users users){
        Query query=new Query(Criteria.where("uid").is(users.getUid()).where("uname").is(users.getUname()));
        return mongoTemplate.remove(query, Users.class);
    }

    /**
     * 分页
     * @param uname
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping("/findByPage")
    public List<Users> findByPage(String uname,Integer pageIndex,Integer pageSize){
        Query query=new Query();

        Criteria criteria=new Criteria();
        ///模糊查询
        Pattern pattern=Pattern.compile("^.*"+uname+".*$");
        criteria.andOperator(criteria.where("uname").regex(pattern));
        query.addCriteria(criteria);

        query.with(Sort.by(Sort.Direction.DESC,"uid"));//排序
        long count = mongoTemplate.count(query, Users.class);
        System.out.println("总记录数："+count);
        System.out.println(pageIndex+"/"+pageSize);
        query.skip((pageIndex-1)*pageSize);//越过多少行
        query.limit(pageSize);

        return  mongoTemplate.find(query,Users.class);
    }


}
